<template>
  <div class="mod-downloadapply">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="initPage()">
      <el-form-item>
        <el-input v-model="searchForm.fileName" placeholder="请输入文件名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          filterable
          end-placeholder="结束时间"
          range-separator="至"
          start-placeholder="开始时间"
          style="width: 100%"
          type="datetimerange"
          @change="searfileCreateTimeChange"
          v-model="fileCreateTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="initPage()">查询</el-button>
        <el-button v-if="isAuth('sys:downloadapply:save')" type="primary" @click="filesDowloadCalc()">生成下载链接</el-button>
        <el-button v-if="isAuth('sys:downloadapply:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="fileName"
        header-align="center"
        align="center"
        label="文件名称">
      </el-table-column>
      <el-table-column
        prop="applyReason"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="申请事由">
      </el-table-column>
      <el-table-column
        prop="fileStartTime"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="生成开始时间"
        :formatter="formatColumnDate">
      </el-table-column>
      <el-table-column
        prop="fileEndTime"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="生成结束时间"
        :formatter="formatColumnDate">
      </el-table-column>
      <el-table-column
        prop="expTime"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="有效期">
      </el-table-column>
      <el-table-column
        prop="times"
        header-align="center"
        align="center"
        label="下载次数">
      </el-table-column>
      <el-table-column
        prop="statusName"
        header-align="center"
        align="center"
        label="申请状态">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="申请时间"
        :formatter="formatColumnDate">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:downloadapply:info')" type="text" size="small"
                     @click="dowloadHandle(scope.row)">下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
import {formatTime} from '@/utils'

export default {
  data() {
    return {
      searchForm: {
        fileName: '',
        createStartTime: '',
        createEndTime: ''
      },
      dataList: [],
      fileCreateTime: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {},
  activated() {
    this.getDataList()
  },
  methods: {
    // 页面初始化
    initPage() {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.$http({
        url: '/sys/downloadapply/list',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'fileName': this.searchForm.fileName,
          'createStartTime': this.searchForm.createStartTime,
          'createEndTime': this.searchForm.createEndTime
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    dowloadHandle(row) {
      this.$confirm(`确定下载该文件?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/sys/downloadapply/dowloadTimes',
          method: 'post',
          params: {
            applyId: row.id
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            debugger
            window.open(row.urlPath, '_blank')
          }
        })
      }).catch(() => {
      })
    },
    // 查看详情
    showDetails(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true)
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/sys/downloadapply/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          }
        })
      }).catch(() => {
      })
    },
    searfileCreateTimeChange(value) {
      if (value) {
        this.searchForm.createStartTime = value[0] + ':00'
        this.searchForm.createEndTime = value[1] + ':00'
      } else {
        this.searchForm.createStartTime = ''
        this.searchForm.createEndTime = ''
      }
    },
    formatColumnDate(row, column) {
      var date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return formatTime(date, 'yyyy-MM-dd')
    },
    filesDowloadCalc() {
      this.$confirm(`确定进行手动[生成下载链接]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: `/sys/downloadapply/filesDowloadCalc`,
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.visible = false
            this.$emit('refreshDataList')
          }
        }).catch(() => {
        })
      })
    }
  }
}
</script>
